﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.Mail;

namespace Project_mfa.Admin
{
    public partial class Admin : System.Web.UI.Page
    {

        private MembershipUser usermemb;
        private Logging log = new Logging();


        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
        public static string[] compUsers(string prefixText, int count, string contextKey)
        {
            MembershipUserCollection users = Membership.GetAllUsers();
            List<String> usersOut = new List<string>();
            foreach (MembershipUser item in users)
            {
                if (item.ToString().ToLower().Contains(prefixText.ToLower()) && item.ToString().ToLower() != HttpContext.Current.User.Identity.Name)
                    usersOut.Add(item.ToString());
            }

            return usersOut.ToArray();
        }

        protected void goUser_Click(object sender, EventArgs e)
        {
            try
            {
                Session["userSelected"] = "false";
                usermemb = Membership.GetUser(users.Text);
                Session["member"] = usermemb;
                txtUserName.Text = usermemb.UserName;
                txtMail.Text = usermemb.Email;

                rdoAdmin.Selected = false;
                rdoLees.Selected = false;
                rdoSchijf.Selected = false;

                if (Roles.IsUserInRole(usermemb.UserName, "Admin"))
                    rdoAdmin.Selected = true;
                else if (Roles.IsUserInRole(usermemb.UserName, "UserLees"))
                    rdoLees.Selected = true;
                else if (Roles.IsUserInRole(usermemb.UserName, "UserSchrijf"))
                    rdoSchijf.Selected = true;

                Session["userSelected"] = "true";
            }
            catch (Exception ex)
            {
                log.WriteLine("Admin", ex.Message);
                System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE='JavaScript'>alert('User not found')</SCRIPT>");
            }
        }

        protected void changeMail_Click(object sender, EventArgs e)
        {
            RegularExpressionValidator1.Validate();
            if (RegularExpressionValidator1.IsValid)
            {
                try
                {
                    if ((string)Session["userSelected"] == "true" && RegularExpressionValidator1.IsValid)
                    {
                        usermemb = (MembershipUser)Session["member"];
                        String newMail = txtMail.Text;
                        usermemb.Email = txtMail.Text;
                        Membership.UpdateUser(usermemb);
                    }
                }
                catch (Exception ex)
                {
                    log.WriteLine("Admin", ex.Message);
                }
            }
        }

        protected void changeRole_Click(object sender, EventArgs e)
        {
            try
            {
                if ((string)Session["userSelected"] == "true")
                {
                    usermemb = (MembershipUser)Session["member"];
                    foreach (var role in Roles.GetAllRoles())
                    {
                        if (Roles.IsUserInRole(usermemb.UserName, role))
                            Roles.RemoveUserFromRole(usermemb.UserName, role);
                    }

                    if (rdoAdmin.Selected)
                        Roles.AddUserToRole(usermemb.UserName, "Admin");
                    else if (rdoLees.Selected)
                        Roles.AddUserToRole(usermemb.UserName, "UserLees");
                    else if (rdoSchijf.Selected)
                        Roles.AddUserToRole(usermemb.UserName, "UserSchrijf");
                }
            }
            catch (Exception ex)
            {
                log.WriteLine("Admin", ex.Message);
            }

        }

        protected void resetPassword_Click(object sender, EventArgs e)
        {
            try
            {
                if ((string)Session["userSelected"] == "true")
                {
                    usermemb = (MembershipUser)Session["member"];
                    String pass = usermemb.ResetPassword();
                    email(pass);
                }
            }
            catch (Exception ex)
            {
                log.WriteLine("Admin", ex.Message);
            }
        }

        protected void DeleteUser_Click(object sender, EventArgs e)
        {
            try
            {
                if ((string)Session["userSelected"] == "true")
                {
                    usermemb = (MembershipUser)Session["member"];
                    Membership.DeleteUser(usermemb.UserName);
                    Response.Redirect(Request.Url.PathAndQuery, true);
                }
            }
            catch (Exception ex)
            {
                log.WriteLine("Admin", ex.Message);
            }
        }

        protected void createUser_Click(object sender, EventArgs e)
        {
            RegularExpressionValidator2.Validate();
            RegularExpressionValidator3.Validate();
            if (RegularExpressionValidator2.IsValid && RegularExpressionValidator3.IsValid)
            {
                try
                {
                    String newUserName = txtNewName1.Text;
                    String newPassword = txtNewPass1.Text;
                    String newPassword2 = txtNewPass2.Text;
                    String newEmail = txtNewMail.Text;
                    if (newPassword.Equals(newPassword2))
                    {
                        String newuserPass = newPassword;
                        Membership.CreateUser(newUserName, newPassword, newEmail);
                    }
                }
                catch (Exception ex)
                {
                    log.WriteLine("Admin", ex.Message);
                }
            }
        }

        private void email(String pass)
        {
            try
            {
                usermemb = (MembershipUser)Session["member"];

                String from = "info@mfa.be";
                String to = usermemb.Email;
                String subject = "mfa password reset";
                String body = "Your new password is: " + pass;
                SmtpMail.SmtpServer = "localhost";
                SmtpMail.Send(from, to, subject, body);
            }
            catch (Exception ex)
            {
                log.WriteLine("Admin", ex.Message);
            }
        }
    }
}